package com.eternal.device;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;

import com.afollestad.materialdialogs.MaterialDialog;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.eternal.base.router.RouterActivityPath;

import com.eternal.device.databinding.ActivityGuideBinding;
import com.eternal.device.model.GuideModel;
import com.eternal.framework.binding.command.BindingConsumer;
import com.eternal.framework.bus.Messenger;
import com.eternal.framework.component.BaseActivity;

/** 添加设备页
 * @author Administrator
 */
@Route(path = RouterActivityPath.Device.PAGE_GUIDE)
public class GuideActivity extends BaseActivity<ActivityGuideBinding, GuideModel> {
    public static final String SHOW_DIALOG = "show dialog";
    public static final String PAGE_TYPE = "page type";

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_guide;
    }

    @Override
    public int initVariableId() {
        return BR.guideModel;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent=getIntent();
        byte pageType = intent.getByteExtra(PAGE_TYPE, (byte) 0);
        viewModel.init(pageType);
        initView(pageType);
        initMessage();
    }

    private void initMessage(){
        Messenger.getDefault().register(this, SHOW_DIALOG, GuideModel.class, new BindingConsumer<GuideModel>() {
            @Override
            public void call(GuideModel itemModel) {
                View view = LayoutInflater.from(GuideActivity.this).inflate(com.eternal.res.R.layout.dialog_tip, null, false);;
                final MaterialDialog dialog = new MaterialDialog.Builder(GuideActivity.this)
                        .backgroundColor(0x00ffffff)
                        .customView(view, false)
                        .build();
                view.findViewById(com.eternal.res.R.id.tv_cancel)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                            }
                        });
                view=dialog.getCustomView();
                view.findViewById(com.eternal.res.R.id.tv_confirm)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                               viewModel.pushToMain();
                               dialog.dismiss();
                            }
                        });
                dialog.show();
            }
        });
    }

    private void initView(byte pageType) {
        if (pageType == 0) {
            binding.tvTitle.setText("PLUG IN CONTROLLER");
            binding.tvContent.setText("Your mobile device and your controller\nmust be within 5 feet of distance.");
            binding.tvContent.setGravity(Gravity.CENTER_HORIZONTAL);
            binding.animationView.setAnimation(R.raw.bluetooth_add1);
        } else {
            binding.tvTitle.setText("ACTIVATE BLUETOOTH");
            binding.tvContent.setText("Hold the port button for 5 to \n" +
                    "activate the Bluetooth. \n" +
                    "Wait for the Bluetooth icon to start flashing on \n" +
                    "the controller’s screen.");
            binding.tvContent.setGravity(Gravity.LEFT);
            binding.animationView.setAnimation(R.raw.bluetooth_add2);
        }
    }

    @Override
    public void onBackPressed() {
        viewModel.onBack.execute();
    }

    private void unregisterMassage(){
        Messenger.getDefault().unregister(this);
    }

    @Override
    protected void onDestroy() {
        unregisterMassage();
        super.onDestroy();
    }
}
